import {
	defineStore
} from 'pinia';
import cartApi from '@/sheep/api/cart';
import _ from 'lodash';

const cart = defineStore('cart', {
	state: () => ({
		list: [], // 购物车列表
		selectedItems: [], // 已选列表
		newList: [], // 除去已下架的购物车列表
		isAllSelected: false, //是否全选
		goodsCount: 0, //购物袋数量
		soldOutItems: [], //下架商品数
		activeItems: [], //可选商品
	}),
	getters: {},
	actions: {
		/**
		 * 获取购物车列表
		 *  @param {object} params 查询参数
		 */
		async getList() {
			const {
				data
			} = await cartApi.getCardList();

			this.list = this.dealWithData(data.cartItems);
			this.soldOutItems = this.list.filter((item) => item.status == 30);
			this.activeItems = this.list.filter((item) => item.status != 30);
			console.log('soldOutItems', this.soldOutItems)
			console.log('activeItems', this.activeItems)
			if (this.list.length === 0) {
				return uni.showToast({
					title: '暂未查询到相关数据',
					icon: 'none',
				});
			}
			this.isAllSelected = false;
			this.selectedItems = []
			this.goodsCount = data.count
		},
		/**
		 * 更新购物车
		 * @param {Object} goodsInfo
		 */
		async update(goodsInfo) {
			// TODO

			this.list.forEach(item => {
				if (item.id === goodsInfo[0].id) {
					console.log(item.id, "====", goodsInfo[0].id)
					item.count = goodsInfo[0].count
					item.printTypes = this.renameList(goodsInfo[0].printTypes)
				}
			})
			console.log(this.list)
		},
		/**
		 * 移除购物车
		 * @param {Object} ids
		 */
		async delete(ids) {
			// TODO
		},

		/**
		 * 选择购物车商品
		 * @param {Object} goodsId
		 */
		selectSingle(goodsId) {
			if (!this.selectedItems.includes(goodsId)) {
				this.selectedItems.push(goodsId);
			} else {
				this.selectedItems.splice(this.selectedItems.indexOf(goodsId), 1);
			}
			this.isAllSelected = this.selectedItems.length === this.list.length - this.soldOutItems.length;
		},

		/**
		 * 全选
		 * @param {Object} flag
		 */
		selectAll(flag) {
			this.isAllSelected = flag;
			this.selectedItems = [];
			if (flag) {
				this.list.forEach((item) => {
					if (this.editMode) {
						if (item.status === 'down') {
							this.selectedItems.push(item.id);
						}
					}
					if (item.status !== 'down') {
						this.selectedItems.push(item.id);
					}
				});
			}
		},
		/**
		 * 返回数据处理
		 */
		dealWithData(list) {
			return list.map((item) => {
				return {
					...item,
					printTypes: this.renameList(item.printTypes),
					checked: false,
					right: 0, //滑动距离
				};
			});
		},
		/**
		 *下拉框数据处理
		 */
		renameList(list) {
			return list.map((itemValue) => {
				return {
					id: itemValue.id,
					value: itemValue.name,
					text: itemValue.nickname,
				};
			})
		},
		/**
		 * 修改全选状态
		 */
		allSelectedChange(flag) {
			this.isAllSelected = flag;
			this.activeItems.forEach((item) => {
				//只针对未下架商品
				item.checked = flag;
			})
			if (flag) {
				this.selectedItems = this.activeItems
			} else {
				this.selectedItems = []
			}
		},
		/**
		 * selectedItems 变化事件
		 */
		selectedItemsChange(goods = []) {
			this.selectedItems = goods;
			console.log('selectedItems', this.selectedItems)
			this.isAllSelected = this.selectedItems.length == this.activeItems.length ? true : false;
			if (this.selectedItems.length > 0) {
				this.activeItems.forEach((item) => {
					item.checked = this.selectedItems.findIndex((element) => element.id == item.id) > -
						1 ? true : false;
				})
			}
		}
	},
	persist: {
		pick: ['list', 'goodsCount'],
	},
});

export default cart;